package com.enhe.gck.fg;

import com.enhe.gck.CarbonBaseConstant;
import com.enhe.gck.CarbonRelationType;
import com.enhe.gck.communal.PublicMethod;
import com.enhe.gck.communal.PublicReturn;
import com.enhe.gck.item.RechargeHWANE16698Item;
import com.enhe.gck.item.UnivalenceHWANE16706Item;
import com.enhe.gck.pojo.*;
import com.kuangkie.carbon.fg.FuncGroupContext;
import com.kuangkie.carbon.fg.ImproveResultFactory;
import com.kuangkie.carbon.fg.SecondRoundImproveFuncGroup;
import com.kuangkie.carbon.fg.ops.ProRecordOpsBuilder;
import com.kuangkie.carbon.record.FGRecordComplexus;
import com.kuangkie.carbon.record.ProRecord;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Repository(value = "hwane28300366042330866425862")
public class BundleRechargeHWANE28300BNB implements SecondRoundImproveFuncGroup {

    @Override
    public void secondImprove(FuncGroupContext context, ProRecord proRecord, FGRecordComplexus recordComplexus,
                              ImproveResultFactory improveResultFactory) {
        if (proRecord.isNew()) {
            BundleRecharge bundleRecharge = proRecord.getBean(BundleRecharge.class);
            List<BundleRechargeDetail> bundleRechargeDetails = Optional
                    .ofNullable(recordComplexus.getGuestProRecords(proRecord.getModelCode(), proRecord.getRecordCode(), CarbonRelationType.RR_组合充值_组合充值记录_组合充值明细))
                    .orElse(Collections.emptyList())
                    .stream().map(r -> {
                        BundleRechargeDetail detail = r.getBean(BundleRechargeDetail.class);
                        detail.setBank(recordComplexus.getGuestProRecord(r.getModelCode(), r.getRecordCode(), CarbonRelationType.RR_组合充值明细_组合充值明细_银行账户).getBean(Bank.class));
                        return detail;
                    })
                    .collect(Collectors.toList());
            if (CollectionUtils.isEmpty(bundleRechargeDetails)) {
                PublicMethod.returnMessage(improveResultFactory, PublicReturn.BUNDLE_RECHARGE_1);
                return;
            }
            Customer customer = recordComplexus.getGuestProRecord(proRecord.getModelCode(), proRecord.getRecordCode(), CarbonRelationType.RR_组合充值_客户组合充值_客户管理).getBean(Customer.class);
            Goods goods = recordComplexus.getGuestProRecord(proRecord.getModelCode(), proRecord.getRecordCode(), CarbonRelationType.RR_组合充值_充值的产品_产品管理).getBean(Goods.class);
            bundleRechargeDetails.forEach(bundleRechargeDetail -> {
                ProRecordOpsBuilder builder = improveResultFactory.getProRecordOpsBuilder(CarbonBaseConstant.合同单价_htdj)
                        .putAttribute(UnivalenceHWANE16706Item.基本属性组_单价, bundleRecharge.getUnit())
                        .putAttribute(UnivalenceHWANE16706Item.基本属性组_税率, bundleRecharge.getTaxRate())
                        .addRelation(CarbonRelationType.RR_合同单价_产品_产品管理, goods.getRecordCode());

                improveResultFactory.getProRecordOpsBuilder(CarbonBaseConstant.充值记录_czjl)
                        .putAttribute(RechargeHWANE16698Item.基本属性组_充值金额, bundleRechargeDetail.getRechargeAmount())
                        .putAttribute(RechargeHWANE16698Item.基本属性组_当前币种金额, bundleRechargeDetail.getRechargeAmount())
                        .putAttribute(RechargeHWANE16698Item.基本属性组_充值类型, bundleRecharge.getType())
                        .putAttribute(RechargeHWANE16698Item.基本属性组_币种, bundleRechargeDetail.getCurrency())
                        .putAttribute(RechargeHWANE16698Item.基本属性组_名称, bundleRechargeDetail.getNote())
                        .addRelation(CarbonRelationType.RR_充值记录_客户名称_客户管理, customer.getRecordCode())
                        .addRelation(CarbonRelationType.RR_充值记录_银行账户_银行账户, bundleRechargeDetail.getBank().getRecordCode())
                        .addRelation(CarbonRelationType.RR_充值记录_属于合同单价_合同单价, builder.getRecordCode());
            });
        }
    }

}
